package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.PatternMatchingUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.ui.widget.IovButton;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.RequestParamConst;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterIntentConst;
import cn.sirun.com.fca.personal.center.model.request.ChangeAccountResendRequest;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.IChangeBindAccountPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.ChangeBindAccountPresenter;
import cn.sirun.com.fca.personal.center.view.IChangeBindAccountView;

public class ChangeBindAccountFlowView extends BaseLoadingFlowView<IChangeBindAccountPresenter>
        implements IChangeBindAccountView {

    private TextView mAccountTV;
    private LinearLayout mEditAccountLL;
    private LineEditText mAccountET;
    private IovButton mClearBTN;
    private String mAccount;

    public ChangeBindAccountFlowView(Activity activity) {
        super(activity);
    }

    public ChangeBindAccountFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();

        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.change_bind_email), ResourcesUtils.getDrawable(R.color.white));
        toolbar.getmMenuTextView().setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
        toolbar.setMenu(R.string.title_modify, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleMenuClick(toolbar);
            }
        });
    }

    @Override
    public void onbindAccountResult(BaseResponse<SubmitPhoneResponse> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            Intent intent = Router.fromPath(AccountActivityConst.Path.PERSONAL_CAPTCHA);
            intent.putExtra(AccountIntentConst.FROM, PersonalCenterIntentConst.MOIDFY_BIND_ACCOUNT);
            intent.putExtra(AccountIntentConst.TELEPHONE, mAccount);
            ActivityUtils.startActivityForResult(getActivity(), intent);
            return;
        }
        AccountStatusUtils.showToast(getActivity(), code);
    }

    @Override
    protected IChangeBindAccountPresenter createPresenter() {
        return new ChangeBindAccountPresenter();
    }

    private void initView() {
        setContentView(R.layout.change_bind_account_activity);
        final Activity activity = getActivity();
        mAccountTV = activity.findViewById(R.id.tv_account);
        mEditAccountLL = activity.findViewById(R.id.ll_edit_account);
        mAccountET = activity.findViewById(R.id.et_account);
        mClearBTN = activity.findViewById(R.id.btn_clear);

        mClearBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAccountET.setText("");
            }
        });
        initData();
    }

    private void handleMenuClick(IovToolbar toolbar) {
        CharSequence text = toolbar.getmMenuTextView().getText();
        if (ResourcesUtils.getString(R.string.title_modify).equals(text.toString())) {
            toolbar.setMenuText(R.string.title_complete);
            mAccountTV.setVisibility(View.GONE);
            mEditAccountLL.setVisibility(View.VISIBLE);
        } else {
//            Intent intent = Router.fromPath(AccountActivityConst.Path.PERSONAL_CAPTCHA);
//            ActivityUtils.startActivityForResult(getActivity(), intent);
            hanldeCompleteViewClick();
        }
    }

    private void hanldeCompleteViewClick() {
        mAccount = mAccountET.getText().toString();
        boolean isPhoneOrEmailCorrect = PatternMatchingUtils.isTelephoneOrEmailCorrect(mAccount);
        if (isPhoneOrEmailCorrect) {
            showLoadingView();
            boolean isPhone = PatternMatchingUtils.isTelephoneCorrect(mAccount);
            ChangeAccountResendRequest request;
            if (isPhone) {
                request =
                        new ChangeAccountResendRequest(RequestParamConst.DEFAULT_NATION_CODE, mAccount);
            } else {
                request = new ChangeAccountResendRequest(mAccount);
            }
            getPresenter().submitBindAccount(request);
        }
    }

    private void initData() {
        PersonalInfo info = PersonalInfoManager.getInstance().getPersonalInfo();
        if (info != null) {
            mAccountTV.setText(info.getEmail());
            mAccountET.setText(info.getEmail());
        }
    }

}
